﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Net;
using Newtonsoft.Json;

namespace Piedone.WebServices
{
    public class ServiceStatusCodeJsonResult : JsonResult
    {
        private readonly HttpStatusCode _statusCode;
        private string _statusDescription;

        public ServiceStatusCodeJsonResult(HttpStatusCode statusCode, string statusDescripton)
        {
            _statusCode = statusCode;
            _statusDescription = statusDescripton;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.StatusCode = (int)_statusCode;
            //context.HttpContext.Response.Status = _statusDescription;

            var response = new Dictionary<string, Dictionary<string, string>>();
            response["error"] = new Dictionary<string,string>
            {
                {"message", _statusDescription}
            };
            context.HttpContext.Response.Write(JsonConvert.SerializeObject(response));

            base.ExecuteResult(context);
        }
    }
}
